﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VIB.Model;
using System.Text.RegularExpressions;

namespace VIB.View
{
    /// <summary>
    /// Interaction logic for EditUser.xaml
    /// </summary>
    public partial class EditUser : Window
    {
        private bool changed = false;

        private User currentUser = new User(-1, String.Empty, String.Empty, String.Empty);

        private int oldIdUser = -1;

        public User CurrentUser
        {
            get 
            { 
                return currentUser; 
            }
            set 
            {
                //save oldUserId so we know which user to update in database
                oldIdUser = ((User)value).IdUser;
                currentUser = value;
            }
        }       

       
        
        public EditUser()
        {
            InitializeComponent();
        }

        public EditUser(User user)
        {
            InitializeComponent();
            CurrentUser = user;
            textbox_iduser.Text = user.IdUser.ToString();
            textbox_firstname.Text = user.Firstname;
            textbox_lastname.Text = user.LastName;
            textbox_mail.Text = user.Email;
        }

        private void RefreshControlStatus(object sender, TextChangedEventArgs e)
        {
            int temp;
            if (!Int32.TryParse(textbox_iduser.Text, out temp))
            {
                textblock_error.Text = "Keine gültige Personalnummer";
                button_saveUserInfo.IsEnabled = false;
            }
            else
            {
                textblock_error.Text = String.Empty;
                button_saveUserInfo.IsEnabled = true;
            }

            //did something change from the old values?
            if (textbox_iduser.Text != CurrentUser.IdUser.ToString() ||
                textbox_firstname.Text != CurrentUser.Firstname ||
                textbox_lastname.Text != CurrentUser.LastName ||
                textbox_mail.Text != CurrentUser.Email)
            {
                changed = true;
                button_saveUserInfo.IsEnabled = true;
            }
            else
            {
                changed = false;
                button_saveUserInfo.IsEnabled = false;
            }

            if (!Regex.IsMatch(textbox_mail.Text, @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"))
            {
                textblock_error.Text = "Bitte gültige E-Mail Adresse eingeben";
                button_saveUserInfo.IsEnabled = false;
            }

            if (textbox_firstname.Text == String.Empty || textbox_lastname.Text == String.Empty || textbox_mail.Text == String.Empty)
            {
                textblock_error.Text = "Bitte vollständig ausfüllen";
                button_saveUserInfo.IsEnabled = false;
            }

            
        }

        private void SaveUserInfo(object sender, RoutedEventArgs e)
        {
            if (changed)
            {
                this.DialogResult = true;
                //create new User object with changed attributes
                User temp = new User(Int32.Parse(textbox_iduser.Text), textbox_firstname.Text, textbox_lastname.Text, textbox_mail.Text);
                try
                {
                    DataManager.EditUser(temp, oldIdUser);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                
                this.Close();
            }
        }

        private void Cancel(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
